package com.alipay.sofa.demo.db.controller;

import com.alipay.sofa.demo.db.common.entity.StudentInfo;
import com.alipay.sofa.demo.db.common.service.StudentInsertService;
import com.alipay.sofa.demo.db.common.service.StudentQueryService;
import com.alipay.sofa.runtime.api.annotation.SofaReference;
import com.alipay.sofa.runtime.api.annotation.SofaReferenceBinding;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @Author Zhang Da
 * @Date 2020/2/13 16:08
 * @Version 1.0
 */

@Api(value = "学生信息", tags = "学生信息接口")
@RestController
@RequestMapping("/student")
public class StudentController {
    @SofaReference(interfaceType = StudentQueryService.class, jvmFirst = false, binding = @SofaReferenceBinding(bindingType = "bolt"))
    private StudentQueryService studentQueryService;

    @SofaReference(interfaceType = StudentInsertService.class, jvmFirst = false, binding = @SofaReferenceBinding(bindingType = "bolt"))
    private StudentInsertService studentInsertService;

    private static final Logger logger = LoggerFactory.getLogger(StudentController.class);

    @ApiOperation(value = "根据学号查询学生信息")
    @GetMapping(value = "/{no}")
    public StudentInfo queryStudentByStudentNo(@PathVariable("no") String no){
        return studentQueryService.queryStudentByStudentNo(no);
    }

    @ApiOperation(value = "根据条件查询学生信息")
    @GetMapping(value = "/queryStudentByConditions")
    public List<StudentInfo> queryStudentByConditions( String studentNo,  String collegeName,
                                                        String majorName,  String grade,
                                                        String className,  String admissionTicketNo,
                                                        Double scoreTotal){
        return studentQueryService.queryStudentsByConditions(studentNo, collegeName, majorName, grade,
                className, admissionTicketNo, scoreTotal);
    }

    @ApiOperation(value = "新增一条学生信息")
    @PostMapping(value = "/addOneStudent")
    public int addOneStudent(@RequestBody StudentInfo studentInfo){
        try {
            return studentInsertService.insertStudents(studentInfo);
        } catch (Exception e) {
            logger.error(String.format("addOneStudent fail: %s", e.getMessage()) );
        }
        return 0;
    }

    @ApiOperation(value = "新增多条学生信息")
    @PostMapping(value = "/addStudents")
    public int addStudents(@RequestBody List<StudentInfo> studentInfos){
        try {
            return studentInsertService.insertStudents(studentInfos);
        } catch (Exception e) {
            logger.error(String.format("addStudents fail: %s", e.getMessage()) );
        }
        return 0;
    }

    @ApiOperation(value = "sayHi-test")
    @GetMapping("/sayHi")
    public String sayHi(){
        return "hi";
    }
}
